package com.ziyue.socket.bean;

import com.ziyue.socket.bean.activity.Activity;
import com.ziyue.socket.bean.answer.RecordKey;
import com.ziyue.socket.bean.log.ActionLog;
import com.ziyue.socket.bean.question.Answer;
import com.ziyue.socket.enums.ActivityStatusEnum;
import com.ziyue.socket.enums.RoleEnum;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * Created by wancheng on 2019/4/13.
 */
public class Room {
    /**
     * 房间号码
     */
    private Integer roomId;
    /**
     * 成员 key 手机号码
     */
    private ConcurrentHashMap<String,Member> members = new ConcurrentHashMap<>();
    /**
     * 互动的基础信息
     */
    private ConcurrentHashMap<String,Activity> activities = new ConcurrentHashMap<>();
    /**
     * 正在进行的互动号
     */
    private String currentActivityId ;
    /**
     * 正在进行的题号
     */
    private String currentQuestionId;
    /**
     * 互动中 老师的行为日志
     */
    private List<ActionLog> actionLogs = new ArrayList<>();
    /**
     * 当前互动正在进行的行为
     */
    private ActivityStatusEnum currentAction;
    /**
     * 学生的作答情况  数据结构 (1,2) : 15011191306 : answer
     */
    private ConcurrentHashMap<RecordKey,Map<String,Answer>> answerRecord = new ConcurrentHashMap<>();
    /**
     * 学生完成作答的时间
     */
    private Map<String,Map<String,Date>> studentFinishMap = new HashMap<>();
    /**
     * 获取老师
     * @return
     */
    public Member getTeacher(){
        if (members == null){
            return null;
        }
        List<Member> teachers =roleMap().get(RoleEnum.TEACHER);
        if (teachers != null && teachers.size() > 0){
            return teachers.get(0);
        }
        return null;
    }

    public List<Member> getRoleMembers(RoleEnum roleEnum){
        if (members == null){
            return null;
        }
        List<Member> students =roleMap().get(roleEnum);
        return students;
    }

    /**
     * 分组
     * @return
     */
    public Map<RoleEnum, List<Member>> roleMap(){
        if (members == null){
            return null;
        }
        List<Member> roomMembers = new ArrayList<>();
        members.entrySet().forEach(e->roomMembers.add(e.getValue()));
        Map<RoleEnum, List<Member>> memberMap = roomMembers.stream().collect(Collectors.groupingBy(Member::getRole));
        return memberMap;
    }

    /**
     * 学生添加作答记录
     * @param account
     * @param answer
     */
    public void addAnswer(String account,Answer answer){
        if (answerRecord == null){
            answerRecord = new ConcurrentHashMap<>(16);
        }
        RecordKey recordKey = new RecordKey(answer.getActivityId(),answer.getQuestionId());
        Map<String,Answer> answers = answerRecord.get(recordKey);
        if (answers == null){
            answers = new HashMap<>();
            answerRecord.put(recordKey,answers);
        }
        answers.put(account,answer);
    }
    public void addActionLog(ActionLog actionLog){

    }
    /**
     * 重置互动信息
     * @param activity
     */
    public void resetActivity(Activity activity){
        if (activity.getActivityId() == null){
            return;
        }
        if (activities == null){
            activities = new ConcurrentHashMap<>();
        }
        activities.put(activity.getActivityId(),activity);
    }

    public Integer getRoomId() {
        return roomId;
    }

    public void setRoomId(Integer roomId) {
        this.roomId = roomId;
    }

    public ConcurrentHashMap<String, Member> getMembers() {
        return members;
    }

    public void setMembers(ConcurrentHashMap<String, Member> members) {
        this.members = members;
    }

    public List<ActionLog> getActionLogs() {
        return actionLogs;
    }

    public void setActionLogs(List<ActionLog> actionLogs) {
        this.actionLogs = actionLogs;
    }

    public ConcurrentHashMap<RecordKey, Map<String, Answer>> getAnswerRecord() {
        return answerRecord;
    }

    public void setAnswerRecord(ConcurrentHashMap<RecordKey, Map<String, Answer>> answerRecord) {
        this.answerRecord = answerRecord;
    }

    public ConcurrentHashMap<String, Activity> getActivities() {
        return activities;
    }

    public void setActivities(ConcurrentHashMap<String, Activity> activities) {
        this.activities = activities;
    }

    public String getCurrentActivityId() {
        return currentActivityId;
    }

    public void setCurrentActivityId(String currentActivityId) {
        this.currentActivityId = currentActivityId;
    }

    public String getCurrentQuestionId() {
        return currentQuestionId;
    }

    public void setCurrentQuestionId(String currentQuestionId) {
        this.currentQuestionId = currentQuestionId;
    }

    public Map<String, Map<String, Date>> getStudentFinishMap() {
        return studentFinishMap;
    }

    public void setStudentFinishMap(Map<String, Map<String, Date>> studentFinishMap) {
        this.studentFinishMap = studentFinishMap;
    }

    public ActivityStatusEnum getCurrentAction() {
        return currentAction;
    }

    public void setCurrentAction(ActivityStatusEnum currentAction) {
        this.currentAction = currentAction;
    }
}
